<?php

/**
 * BH MEDIA
 * @author Ha Anh Man - manphp@binhhoang.com
 * @version 1.0
 * @package JobSearch
 * @copyright 2012
 * 
 */
require_once dirname(__FILE__) . '/model/CategoryModel.php';

class CategoryController extends BackendController {

    public function actionIndex() {
        $model = new CategoryModel();
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $listId = $_POST['listId'];
            if (!empty($listId)) {
                $arr = explode(':', $listId);
                foreach ($arr as $item) {
                    $ar = explode('-', $item);
                    $model->updateWeight($ar[0], $ar[1]);
                }
                createMessage('Thay đổi thứ tự hiển thị thành công');
                $this->redirect(array('category/index'));
            }
        }
        $data = array();
        $data['listItem'] = $model->getCategoryParent(0);
        $this->render('index', array('data' => $data));
    }

    public function actionSub() {
        $cate_id = $_GET['parent_id'];
        $model = new CategoryModel();
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $listId = $_POST['listId'];
            if (!empty($listId)) {
                $arr = explode(':', $listId);
                foreach ($arr as $item) {
                    $ar = explode('-', $item);
                    $model->updateWeight($ar[0], $ar[1]);
                }
                createMessage('Thay đổi thứ tự hiển thị thành công');
                $this->redirect(array('category/sub?parent_id=' . $cate_id));
            }
        }
        $data = array();
        $data['listItem'] = $model->getCategoryParent($cate_id);
        $this->render('index', array('data' => $data));
    }

    public function actionEdit() {
        $cate_id = $_GET['id'];
        $model = new CategoryModel();
        $cate = $model->getCategory($cate_id);
        if (empty($cate)) {
            createMessage('Không tìm thấy danh mục bạn yêu cầu', 'errormsg');
            $this->redirect(array('category/index'));
        }


        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $messages = array();
            if (empty($_POST['name'])) {
                $messages[] = 'Vui lòng nhập tên danh mục';
            }
            if (!empty($messages)) {
                createMessage($messages, 'errormsg');
                $data['cate'] = $_POST;
            } else {
                $name = trim($_POST['name']);
                $banner = $cate->banner;
                $icon = $cate->icon;

                $icon_upload = $_FILES['icon'];
                if (!empty($icon_upload['name'])) {
                    if (image_match($icon_upload['name'], $icon_upload['tmp_name'])) {
                        $icon = time() . '_' . vietdecode($icon_upload['name']);
                        move_uploaded_file($icon_upload['tmp_name'], CATEGORY_ICON_PATH . '/' . $icon);

                        //xoa icon cu
                        if (file_exists(CATEGORY_ICON_PATH . '/' . $cate->icon)) {
                            @unlink(CATEGORY_ICON_PATH . '/' . $cate->icon);
                        }
                    }
                }

                $banner_upload = $_FILES['banner'];
                if (!empty($banner_upload['name'])) {
                    if (image_match($banner_upload['name'], $banner_upload['tmp_name'])) {
                        $banner = time() . '_' . vietdecode($banner_upload['name']);
                        move_uploaded_file($banner_upload['tmp_name'], CATEGORY_BANNER_PATH . '/' . $banner);
                        //xoa banner cu
                        if (file_exists(CATEGORY_BANNER_PATH . '/' . $cate->banner)) {
                            @unlink(CATEGORY_BANNER_PATH . '/' . $cate->banner);
                        }
                    }
                }

                $cate->parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
                $cate->name = $name;
                $cate->alias = change_url_seo($name);
                $cate->keyword = trim($_POST['keyword']);
                $cate->description = trim($_POST['description']);
                $cate->banner = $banner;
                $cate->banner_show = intval($_POST['banner_show']);
                $cate->icon = $icon;
                $cate->weight = 0;
                $cate->status = intval($_POST['status']);
                $cate->save();
                createMessage('Sửa danh mục <strong>' . $name . '</strong> thành công');
                if ($cate->parent_id > 0) {
                    $redirect = 'backend/category/sub?parent_id=' . $cate->parent_id;
                    $this->redirect(array($redirect));
                } else {
                    $this->redirect(array('category/index'));
                }
            }
        }


        $data['cate'] = $cate->attributes;
        if ($data['cate']['parent_id'] > 0) {
            $data['listCategory'] = $model->getCategoryParent(0);
        }
        $this->render('form', array('data' => $data));
    }

    public function actionAdd() {
        $data = array();
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $messages = array();
            if (empty($_POST['name'])) {
                $messages[] = 'Vui lòng nhập tên danh mục';
            }
            if (!empty($messages)) {
                createMessage($messages, 'errormsg');
                $data['cate'] = $_POST;
            } else {
                $name = trim($_POST['name']);
                $banner = '';
                $icon = '';

                $icon_upload = $_FILES['icon'];
                if (!empty($icon_upload['name'])) {
                    if (image_match($icon_upload['name'], $icon_upload['tmp_name'])) {
                        $icon = time() . '_' . vietdecode($icon_upload['name']);
                        move_uploaded_file($icon_upload['tmp_name'], CATEGORY_ICON_PATH . '/' . $icon);
                    }
                }

                $banner_upload = $_FILES['banner'];
                if (!empty($banner_upload['name'])) {
                    if (image_match($banner_upload['name'], $banner_upload['tmp_name'])) {
                        $banner = time() . '_' . vietdecode($banner_upload['name']);
                        move_uploaded_file($banner_upload['tmp_name'], CATEGORY_BANNER_PATH . '/' . $banner);
                    }
                }


                $cate = new Category();
                $cate->parent_id = isset($_POST['parent_id']) ? intval($_POST['parent_id']) : 0;
                $cate->name = $name;
                $cate->alias = change_url_seo($name);
                $cate->keyword = trim($_POST['keyword']);
                $cate->description = trim($_POST['description']);
                $cate->banner = $banner;
                $cate->banner_show = intval($_POST['banner_show']);
                $cate->icon = $icon;
                $cate->weight = 0;
                $cate->status = intval($_POST['status']);
                $cate->save();
                createMessage('Thêm mới danh mục <strong>' . $name . '</strong> thành công');
                if (isset($_GET['parent_id'])) {
                    $redirect = 'backend/category/sub?parent_id=' . $_GET['parent_id'];
                    $this->redirect(array($redirect));
                } else {
                    $this->redirect(array('category/index'));
                }
            }
        }
        if (isset($_GET['parent_id'])) {
            $model = new CategoryModel();
            $data['listCategory'] = $model->getCategoryParent(0);
        }
        $this->render('form', array('data' => $data));
    }

    public function actionDelete() {
        $cate_id = $_GET['id'];
        $model = new CategoryModel();
        $cate = $model->getCategory($cate_id);
        if (empty($cate)) {
            createMessage('Không tìm thấy danh mục bạn yêu cầu', 'errormsg');
            $this->redirect(array('category/index'));
        }
        if (!empty($cate->icon)) {
            if (file_exists(CATEGORY_ICON_PATH . '/' . $cate->icon)) {
                @unlink(CATEGORY_ICON_PATH . '/' . $cate->icon);
            }
        }
        if (!empty($cate->banner)) {
            if (file_exists(CATEGORY_BANNER_PATH . '/' . $cate->banner)) {
                @unlink(CATEGORY_BANNER_PATH . '/' . $cate->banner);
            }
        }
        if ($cate->parent_id == 0) {
            $listCate = $model->getCategoryParent($cate->id);            
            if (!empty($listCate)) {
                foreach ($listCate as $item) {
                    if (!empty($item->icon)) {
                        if (file_exists(CATEGORY_ICON_PATH . '/' . $item->icon)) {
                            @unlink(CATEGORY_ICON_PATH . '/' . $item->icon);
                        }
                    }                    
                    $item->delete();
                }
            }
        }
        $cate->delete();
        createMessage('Xóa danh mục <strong>' . $cate->name . '</strong> thành công');
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

}